package day01;

import java.util.concurrent.*;

/**
 * @author feng
 * @Date 2021/7/13 7:07
 */
public class ThreadTest06 implements Callable {
    private static String winnner;

    @Override
    public Boolean call() throws Exception {
        for (int i = 0; i <= 100; i++) {

            //模拟兔子睡觉
            if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //判断
            boolean flag = gameover(i);
            if (flag) {
                break;
            }

            System.out.println(Thread.currentThread().getName() + "跑了" + i + "步");
        }
        return true;
    }


    // 判断比赛是否结束
    private boolean gameover(int i) {

        if (winnner != null) {
            return true;
        }
        {
            if (i >= 100) {
                winnner = Thread.currentThread().getName();
                System.out.println("winner is" + winnner);

            }
        }
        return false;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建实现类对象
        ThreadTest06 t1 = new ThreadTest06();
        ThreadTest06 t2 = new ThreadTest06();

        // 4 创建服务
        ExecutorService es = Executors.newFixedThreadPool(2);
        //5.提交事务
        Future<Boolean> result1 = es.submit(t1);
        Future<Boolean> result2 = es.submit(t2);
        // 6.查看结果
        Boolean r1 = result1.get();
        Boolean r2 = result2.get();
        // 7.关闭线程
        es.shutdownNow();


    }


}


